// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let uName = event.worker
  let uId = event.uuid
  switch (event.action) {
    case 'getUrlScheme': {
      return getUrlScheme(uName, uId)
    }
  }
  return 'action not found'
}

async function getUrlScheme(uName, uId) {
  return cloud.openapi.urlscheme.generate({
    // /pages/index/index?option=default&area=江门&worker=${uName}&uuid=${uId}
    jumpWxa: {
      path: `/page/index/index`, // <!-- replace -->
      query: `?option=default&area=江门&worker=${uName}&uuid=${uId}`,
      envVersion: 'trial'
    },
    // 如果想不过期则置为 false，并可以存到数据库
    isExpire: false,
    // 一分钟有效期
    expireTime: parseInt(Date.now() / 1000 + 60),
  })
}
